package com.atguigu.controller;


import com.github.pagehelper.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public abstract class BaseController {

    protected final static String PAGE_SUCCESS = "common/successPage";

    //提示信息
    public final static String MESSAGE_SUCCESS = "操作成功！";

    /**
     * 成功页
     * @param message
     * @param request
     */
    protected String successPage(String message, HttpServletRequest request) {
        request.setAttribute("messagePage", StringUtil.isEmpty(message) ? MESSAGE_SUCCESS : message);
        return PAGE_SUCCESS;
    }

    /**
     * 获取所有请求参数的辅助方法
     * @param request
     * @return
     */
    protected Map<String, Object> getFilters(HttpServletRequest request) {
        //http://localhost:8000/role?roleName=abc&pageNum=1&pageSize=10&age=22&age=23
        //获取请求参数：例如，角色名称（roleName）,第几页(pageNum),每页多少条(pageSize)
        Enumeration<String> parameterNames = request.getParameterNames();
        //request.getParameterMap()
        Map<String,Object> filters = new HashMap<String,Object>(); //存放所有请求参数：roleName,pageNum,pageSize

        while(parameterNames!=null && parameterNames.hasMoreElements()){
            String parameterName = parameterNames.nextElement();
            String[] values = request.getParameterValues(parameterName);
            if(values.length>1){
                filters.put(parameterName,values);
            }else{
                filters.put(parameterName,values[0]);
            }
        }

        if(!filters.containsKey("pageNum")){
            filters.put("pageNum",1);
        }

        if(!filters.containsKey("pageSize")){
            filters.put("pageSize",2);
        }
        return filters;
    }
}
